import dataStorage from "@ohos.data.storage";
import http from "@ohos.net.http";
import prompt from "@system.prompt";
import router from "@system.router";
import { isPhoneValid } from "../../common/js/utils.js";

export default {
    data: {
        title: "World",
        phone: "",
        code: "",
        timer: 0, // 验证码获取计时
        smsCode: "",
    },
    computed: {
        codeText() {
            // 额，不能用匿名函数
            return this.timer > 0 ? `${this.timer}s重新获取` : "获取验证码";
        },
    },
    goBack() {
        router.back();
    },
    onPhoneChange(ev) {
        this.phone = ev.value;
    },
    onCodeChange(ev) {
        this.code = ev.value;
    },
    startTimer() {
        this.timer = 60;
        let timer = setInterval(() => {
            if (this.timer == 0) {
                clearInterval(timer);
                return;
            }
            this.timer--;
        }, 1000);
    },
    resendCode() {
        this.startTimer();
        // 重新发送
        const req = http.createHttp();
        const promise = req.request("https://trezedo.club/api/ha/user/code/2", {
            method: "POST",
            extraData: {
                phone: this.phone,
            },
        });
        promise.then(({ result }) => {
            console.info("!! " + result);
            result = JSON.parse(result);
            // @ts-ignore
            if (result.code == 200) {
                // @ts-ignore
                this.smsCode = result.data.code;
                prompt.showDialog({
                    // @ts-ignore
                    message: `您正在申请修改密码，您的验证码：${result.data.code}。请在5分钟内输入完成验证。`,
                });
            } else {
                prompt.showToast({
                    // @ts-ignore
                    message: result.msg,
                });
            }
        });
    },
    getVerificationCode() {
        // 若手机号码规则错误或为空，则提示“请输入正确的手机号码”
        if (!isPhoneValid(this.phone)) {
            prompt.showToast({
                message: "请输入正确的手机号码",
            });
            return;
        }
        // todo 若当前设备[获取验证码]的请求数 >10 次，则弹窗提示“获取验证码次数过多，请明日再试！”
        /*prompt.showDialog({
            message: "获取验证码次数过多，请明日再试！",
            buttons: [
                {
                    text: "确定",
                    color: "#09f",
                },
            ],
            success(data) {
                console.log("click button : " + data.index);
            },
            cancel() {
            },
        });*/
        // 若手机号码未注册，则提示“手机号码未注册”
        this.resendCode();
    },
    nextStep() {
        if (!isPhoneValid(this.phone)) {
            prompt.showToast({
                message: "手机号不正确",
            });
            return;
        }
        if (!/^\d{6}$/.test(this.code)) {
            prompt.showToast({
                message: "验证码不正确",
            });
            return;
        }
        // 校验验证码
        if (this.smsCode == this.code) {
            router.push({
                uri: "pages/login_forget2/forget2",
                params: {
                    phone: this.phone,
                    code: this.code,
                },
            });
        } else {
            prompt.showToast({
                message: "验证码错误",
            });
        }
    },
};
